<?php
/**
 * Created by PhpStorm.
 * 商家提现处理
 * User: caicm
 * Date: 2017/6/28
 * Time: 15:51
 */

namespace WMApi\Controller;


use Think\Model;
use WMApi\Model\WmSellerModel;
use WMApi\Service\SellerWithdrawService;

class SellerWithdrawController extends SellerBaseController {

    /**
     * 获取商家金额信息
     * Created By caicm
     */
    public function get_seller_account_money(){
        $withdraw_service = new SellerWithdrawService();
        $data = $withdraw_service->get_seller_account_money($this->seller_id);
        returnApiSuccess($data);
    }

    /**
     * 商家提现申请
     * Created By caicm
     * @param bank_card_id 银行卡id
     * @param withdraw_money 提现金额
     * @param withdraw_password 提现密码
     */
    public function apply(){
        $bank_card_id = I('post.bank_card_id', 0, 'int');
        $withdraw_money = I('post.withdraw_money');
        $withdraw_password = I('withdraw_password');
        if(empty($bank_card_id) || !is_money($withdraw_money) || empty($withdraw_password)){
            returnApiError('参数有误');
        }

        $withdraw_service = new SellerWithdrawService();
        $apply_state = $withdraw_service->apply($this->seller_id, $bank_card_id, $withdraw_money, $withdraw_password);

        if($apply_state){
            returnApiSuccess(array('withdraw_id' => $apply_state), '操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /**
     * 商家提现申请详情
     * Created By caicm
     * @param withdraw_id 提现id
     */
    public function get_withdraw_info(){
        $withdraw_id = I('post.withdraw_id', 0, 'int');
        if(empty($withdraw_id)){
            returnApiError('参数有误');
        }

        $withdraw_service = new SellerWithdrawService();
        $withdraw_info = $withdraw_service->get_withdraw_info($this->seller_id, $withdraw_id);

        if($withdraw_info){
            returnApiSuccess($withdraw_info);
        }else{
            returnApiError('无数据');
        }
    }

    /**
     * 商家提现流程详情
     * Created By caicm
     * @param withdraw_id 提现id
     */
    public function get_withdraw_flow_info(){
        $withdraw_id = I('post.withdraw_id', 0, 'int');
        if(empty($withdraw_id)){
            returnApiError('参数有误');
        }

        $withdraw_service = new SellerWithdrawService();
        $withdraw_info = $withdraw_service->get_withdraw_info($this->seller_id, $withdraw_id);

        $data[] =array(
            'title' => '审核已提交，请等待处理，到款时间为3日',
            'date' => $withdraw_info['withdraw_time'],
            'remark' => '',
        );

        if($withdraw_info['state'] == 2){
            $data[] =array(
                'title' => '审核不通过，金额已退回您的账户',
                'date' => $withdraw_info['audit_time'],
                'remark' => $withdraw_info['audit_remark'],
            );
        }

        if($withdraw_info['state'] == 3){
            $data[] =array(
                'title' => '审核已通过，现金会在3日内到账',
                'date' => $withdraw_info['audit_time'],
                'remark' => '',
            );
        }

        returnApiSuccess(array('list' => $data));

    }

    /**
     * 获取区域列表
     * Created By caicm
     * @param seller_id 商家id
     */
    public function get_region(){
        $withdraw_service = new SellerWithdrawService();
        //检查商家银行卡
        $withdraw_service->check_bank_card_num();
        $data['region_list'] = $withdraw_service->get_region_list($this->seller_id);
        returnApiSuccess($data);
    }

    /**
     * 添加银行卡
     * Created By caicm
     * @param string bank_card_account 银行卡开户人
     * @param string bank_card_number 银行卡号
     * @param string bank_card_dbk 银行卡所属银行
     * @param string bank_card_subbranch 银行卡支行
     * @param int province_id 省份id
     * @param int city_id 城市id
     * @param int district_id 地区id
     */
    public function add_bank_card(){
        $bank_card_account = I('post.bank_card_account');
        $bank_card_number = I('post.bank_card_number');
        $bank_card_dbk = I('post.bank_card_dbk');
        $bank_card_subbranch = I('post.bank_card_subbranch');
        $province_id = I('post.province_id', 0, 'int');
        $city_id = I('post.city_id', 0, 'int');
        $district_id = I('post.district_id', 0, 'int');

        if(empty($bank_card_account) || empty($bank_card_number) || empty($bank_card_dbk) || empty($bank_card_subbranch) || empty($province_id) || empty($city_id) || empty($district_id)){
            returnApiError('参数有误');
        }

        $withdraw_service = new SellerWithdrawService();
        //检查商家银行卡
        $withdraw_service->check_bank_card_num($this->seller_id);
        $add_card = $withdraw_service->add_bank_card($this->seller_id, $bank_card_account, $bank_card_number, $bank_card_dbk, $bank_card_subbranch, $province_id, $city_id, $district_id);

        if($add_card){
            returnApiSuccess(array('bank_card_id' => $add_card), '操作成功');
        }else{
            returnApiError('操作失败');
        }

    }

    /**
     * 获取商家银行卡列表
     * Created By caicm
     */
    public function get_bank_card_list(){
        $withdraw_service = new SellerWithdrawService();
        $card_list = $withdraw_service->get_bank_card_list($this->seller_id);
        if($card_list){
            returnApiSuccess(array('bank_card_list' => $card_list));
        }else{
            returnApiError('无数据');
        }
    }

    /**
     * 获取银行卡详情
     * Created By caicm
     * @param bank_card_id 银行卡id
     */
    public function get_bank_card_info(){

        $bank_card_id = I('post.bank_card_id');
        if(empty($bank_card_id)){
            returnApiError('参数有误');
        }

        $withdraw_service = new SellerWithdrawService();
        $card_info = $withdraw_service->get_bank_card_info($this->seller_id, $bank_card_id);
        if($card_info){
            returnApiSuccess($card_info);
        }else{
            returnApiError('无数据');
        }
    }


}